﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;
using LinqToTwitter;
using TwitterMatic.Shared;
using TwitterMatic.Shared.Domain;

namespace TwitterMatic_WebRole.Controllers
{
    public class AccountController : BaseController
    {
        protected IConfigurationProvider configuration;

        public AccountController()
        {
            configuration = new AzureConfigurationProvider();
        }

        public ActionResult Login(string oauth_token)
        {
            IOAuthTwitter oAuthTwitter = new OAuthTwitter();
            oAuthTwitter.OAuthConsumerKey = configuration.ReadSetting("OAuthConsumerKey");
            oAuthTwitter.OAuthConsumerSecret = configuration.ReadSetting("OAuthConsumerSecret"); 

            if (string.IsNullOrEmpty(oauth_token)) {
                // Not authorized. Redirect to Twitter!
                string loginUrl = oAuthTwitter.AuthorizationLinkGet(
                    configuration.ReadSetting("OAuthRequestTokenUrl"),
                    configuration.ReadSetting("OAuthAuthorizeTokenUrl"),
                    false,
                    true
                );
                return Redirect(loginUrl);
            } else {
                // Should be authorized. Get the access token and secret.
                string userId = "";
                string screenName = "";

                oAuthTwitter.AccessTokenGet(oauth_token, configuration.ReadSetting("OAuthAccessTokenUrl"),
                    out screenName, out userId);
                if (oAuthTwitter.OAuthTokenSecret.Length > 0)
                {
                    // Store the user in membership
                    MembershipUser user = Membership.GetUser(screenName);
                    if (user == null)
                    {
                        MembershipCreateStatus status = MembershipCreateStatus.Success;
                        user = Membership.CreateUser(
                            screenName,
                            oAuthTwitter.OAuthToken + ";" + oAuthTwitter.OAuthTokenSecret,
                            screenName,
                            "twitter",
                            "matic",
                            true,
                            out status);
                    }

                    // Change user's password
                    user.ChangePassword(
                        user.GetPassword("matic"),
                        oAuthTwitter.OAuthToken + ";" + oAuthTwitter.OAuthTokenSecret
                    );
                    Membership.UpdateUser(user);

                    // All is well!
                    FormsAuthentication.SetAuthCookie(screenName, true);
                    return RedirectToAction("Index", "Home");
                } 
                else 
                {
                    // Not OK...
                    return RedirectToAction("Login");
                }
            }
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }
    }
}
